package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PlaneFrame extends JFrame {
    JButton button;
    JPanel panel;
    JLabel userLabel;
    JLabel passLabel;
    JTextField username;
    JPasswordField password;

    public PlaneFrame() {
        this.setTitle("飞机大战 - 登录");
        this.setSize(420, 180);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    void init() {
        panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(6, 6, 6, 6);
        c.fill = GridBagConstraints.HORIZONTAL;

        userLabel = new JLabel("请输入账户:");
        passLabel = new JLabel("请输入密码:");

        username = new JTextField(15);
        password = new JPasswordField(15);

        button = new JButton("开始游戏");
        button.setPreferredSize(new Dimension(100, 30));

        // 布局：标签 - 字段
        c.gridx = 0; c.gridy = 0; c.weightx = 0;
        panel.add(userLabel, c);
        c.gridx = 1; c.gridy = 0; c.weightx = 1.0;
        panel.add(username, c);

        c.gridx = 0; c.gridy = 1; c.weightx = 0;
        panel.add(passLabel, c);
        c.gridx = 1; c.gridy = 1; c.weightx = 1.0;
        panel.add(password, c);

        c.gridx = 1; c.gridy = 2; c.anchor = GridBagConstraints.EAST; c.weightx = 0;
        panel.add(button, c);

        // 验证逻辑
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user = username.getText().trim();
                String pass = new String(password.getPassword());

                if (user.isEmpty() || pass.isEmpty()) {
                    JOptionPane.showMessageDialog(PlaneFrame.this,
                            "账户密码不能为空",
                            "格式错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!user.matches("^[A-Za-z][A-Za-z0-9_]{2,19}$")) {
                    JOptionPane.showMessageDialog(PlaneFrame.this,
                            "用户名格式错误：字母开头，包含字母、数字、下划线，长度3-20位。",
                            "格式错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!pass.matches("^(?=.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).*$")) {
                    JOptionPane.showMessageDialog(PlaneFrame.this,
                            "密码格式错误：至少8位，必须包含大写字母、小写字母和数字。",
                            "格式错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!"admin".equals(user) || !"Admin123".equals(pass)) {
                    JOptionPane.showMessageDialog(PlaneFrame.this,
                            "账户或密码错误",
                            "登录失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                dispose();
                JFrame jFrame = new JFrame("飞机大战");
                jFrame.setSize(800, 600);
                jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jFrame.setVisible(true);
            }
        });

        this.add(panel);
    }

    public class LoginException extends RuntimeException {
        public LoginException(String message) {
            super(message);
        }
    }

}
